﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace LifeInSharePoint.EUCookie.Layouts.LifeInSharePoint.EUCookie.admin
{
    using System.Web;
    using System.Web.UI.WebControls;

    using Microsoft.SharePoint.Utilities;

    /// <summary>
    /// The cookie cuttr.
    /// </summary>
    public partial class CookieCuttr : LayoutsPageBase
    {

        #region Constants

        private const string ToggleMessagesDefaultValue = "true";

        private const string AcceptCookieButtonTextDefaultValue = "ACCEPT COOKIES";
        private const string DeclineCookieButtonTextDefaultValue = "DECLINE COOKIES";
        private const string ResetCookieButtonTextDefaultValue = "RESET COOKIES FOR THIS WEBSITE";
        private const string WhatAreCookieLinkTextDefaultValue = "What are Cookies?";
        private const string DiscreetLinkTextDefaultValue = "Cookies?";

        private const string AnalyticsMessageDefaultValue = "We use cookies, just to track visits to our website, we store no personal details.";
        private const string CookiePrivacyLinkMessageDefaultValue = "We use cookies on this website, you can <a href='{{cookiePolicyLink}}' title='read about our cookies'>read about them here</a>. To use the website as intended please...";
        private const string ErrorMessageDefaultValue = "We're sorry, this feature places cookies in your browser and has been disabled. <br>To continue using this functionality, please";
        private const string PrivacyPolicyMessageDefaultValue = "Please read the information below and then choose from the following options";
        private const string DifferentMessageOnPolicyPageDefaultValue = "false";

        private const string PrivacyPolicyLinkDefaultValue = "/privacy-policy/";
        private const string WhatAreCookiesLinkDefaultValue = "http://www.allaboutcookies.org/";

        private const string ShowAcceptButtonDefaultValue = "true";
        private const string ShowDeclineButtonDefaultValue = "true";
        private const string ShowResetButtonDefaultValue = "false";
        
        #endregion

        #region Private Properties

        private string GoogleAnalyticsIDPBValue;
        private bool EnableGoogleAnalyticsUntilDeclinedPBValue;
        private string DomainNamePBValue;

        private bool ToggleMessagesPBValue;

        private string AcceptCookieButtonTextPBValue;
        private string DeclineCookieButtonTextPBValue;
        private string ResetCookieButtonTextPBValue;
        private string WhatAreCookieLinkTextPBValue;
        private string DiscreetLinkTextPBValue;

        private bool ShowAcceptButtonPBValue;
        private bool ShowDeclineButtonPBValue;
        private bool ShowResetButtonPBValue;

        private bool DisplayDiscreetLinkPBValue;
        private bool UseLargeOverlayPBValue;

        private string AnalyticsMessagePBValue;
        private string CookiePrivacyLinkMessagePBValue;
        private string ErrorMessagePBValue;
        private string PrivacyPolicyMessagePBValue;
        private bool DifferentMessageOnPolicyPagePBValue;

        private string PrivacyPolicyLinkPBValue;
        private string WhatAreCookiesLinkPBValue;

        private bool DisplayNotificationAtBottomOfPagePBValue;
        private string DiscreetLinkPositionPBValue;


        private bool EnablePageElementDisablerPBValue;
        private bool ActivatePageDisablerWhenVisitorDeclinesPBValue;
        private string DisabledCSSSelectorsPBValue;


        #endregion

        #region Protected Methods

        /// <summary>
        /// The page_ load.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        protected void Page_Load(object sender, EventArgs e)
        {
            BtnApply.Click += new EventHandler(this.BtnApply_Click);
            BtnSave.Click += new EventHandler(this.BtnSave_Click);
            this.SetDropDownValues();
            this.GetPropertyBagValues();
        }

        /// <summary>
        /// The on pre render.
        /// </summary>
        /// <param name="e">
        /// The e.
        /// </param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            this.SetFormValues();
        }

        #endregion

        /// <summary>
        /// The set drop down values.
        /// </summary>
        private void SetDropDownValues()
        {
            this.ddlDiscreetLinkPosition.Items.Add(new ListItem("Top Left", "topleft"));
            this.ddlDiscreetLinkPosition.Items.Add(new ListItem("Top Right", "topright"));
            this.ddlDiscreetLinkPosition.Items.Add(new ListItem("Bottom Left", "bottomleft"));
            this.ddlDiscreetLinkPosition.Items.Add(new ListItem("Bottom Right", "bottomright"));
        }

        /// <summary>
        /// The get and check value.
        /// </summary>
        /// <param name="defaultValue">
        /// The default value.
        /// </param>
        /// <param name="PropertyBagParam">
        /// The property bag param.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        private string GetAndCheckValueString(string defaultValue, string PropertyBagParam)
        {
            string PBValue = Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, PropertyBagParam);
            if (string.IsNullOrEmpty(PBValue))
            {
                PBValue = defaultValue;
            }

            return PBValue;
        }

        /// <summary>
        /// The set form values.
        /// </summary>
        private void SetFormValues()
        {
            this.txtGoogleAnalyticsID.Text = this.GoogleAnalyticsIDPBValue;
            this.cbxEnableGoogleAnalyticsUntilDeclined.Checked = this.EnableGoogleAnalyticsUntilDeclinedPBValue;
            this.txtDomainName.Text = this.DomainNamePBValue;

            this.cbxToggleMessages.Checked = this.ToggleMessagesPBValue;

            this.txtAcceptCookieButtonText.Text = this.AcceptCookieButtonTextPBValue;
            this.txtDeclineCookieButtonText.Text = this.DeclineCookieButtonTextPBValue;
            this.txtResetCookieButtonText.Text = this.ResetCookieButtonTextPBValue;
            this.txtWhatAreCookieLinkText.Text = this.WhatAreCookieLinkTextPBValue;
            this.txtDiscreetLinkText.Text = this.DiscreetLinkTextPBValue;

            this.cbxShowAccept.Checked = this.ShowAcceptButtonPBValue;
            this.cbxShowDecline.Checked = this.ShowDeclineButtonPBValue;
            this.cbxShowReset.Checked = this.ShowResetButtonPBValue;

            this.cbxDisplayDiscreetLink.Checked = this.DisplayDiscreetLinkPBValue;
            this.cbxUseLargeOverlay.Checked = this.UseLargeOverlayPBValue;

            this.txtAnalyticsMessage.Text = this.AnalyticsMessagePBValue;
            this.txtCookiePrivacyLinkMessage.Text = this.CookiePrivacyLinkMessagePBValue;
            this.txtErrorMessage.Text = this.ErrorMessagePBValue;
            this.txtPrivacyPolicyMessage.Text = this.PrivacyPolicyMessagePBValue;
            this.cbxDifferentMessageOnPolicyPage.Checked = this.DifferentMessageOnPolicyPagePBValue;

            this.txtPrivacyPolicyLink.Text = this.PrivacyPolicyLinkPBValue;
            this.txtWhatAreCookiesLink.Text = this.WhatAreCookiesLinkPBValue;

            this.cbxDisplayNotificationAtBottomOfPage.Checked = this.DisplayNotificationAtBottomOfPagePBValue;
            this.ddlDiscreetLinkPosition.SelectedValue = this.DiscreetLinkPositionPBValue;

            this.cbxEnablePageElementDisabler.Checked = this.EnablePageElementDisablerPBValue;
            this.cbxActivatePageDisablerWhenVisitorDeclines.Checked = this.ActivatePageDisablerWhenVisitorDeclinesPBValue;
            this.txtDisabledCSSSelectors.Text = this.DisabledCSSSelectorsPBValue;
        }

        /// <summary>
        /// Gets the Values from the Property Bag to be used within the Admin Page
        /// </summary>
        private void GetPropertyBagValues()
        {
            this.GoogleAnalyticsIDPBValue = this.GetAndCheckValueString(string.Empty, "GoogleAnalyticsIDPBValue");
            if (!string.IsNullOrEmpty(Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, "EnableGoogleAnalyticsUntilDeclinedPBValue")))
            {
                this.EnableGoogleAnalyticsUntilDeclinedPBValue = Convert.ToBoolean(Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, "EnableGoogleAnalyticsUntilDeclinedPBValue"));
            }

            this.DomainNamePBValue = this.GetAndCheckValueString(string.Empty, "DomainNamePBValue");

            this.ToggleMessagesPBValue = Convert.ToBoolean(this.GetAndCheckValueString(ToggleMessagesDefaultValue, "ToggleMessagesPBValue"));

            this.AcceptCookieButtonTextPBValue = this.GetAndCheckValueString(AcceptCookieButtonTextDefaultValue, "AcceptCookieButtonTextPBValue");
            this.DeclineCookieButtonTextPBValue = this.GetAndCheckValueString(DeclineCookieButtonTextDefaultValue, "DeclineCookieButtonTextPBValue");
            this.ResetCookieButtonTextPBValue = this.GetAndCheckValueString(ResetCookieButtonTextDefaultValue, "ResetCookieButtonTextPBValue");
            this.WhatAreCookieLinkTextPBValue = this.GetAndCheckValueString(WhatAreCookieLinkTextDefaultValue, "WhatAreCookieLinkTextPBValue");
            this.DiscreetLinkTextPBValue = this.GetAndCheckValueString(DiscreetLinkTextDefaultValue, "DiscreetLinkTextPBValue");


            this.ShowAcceptButtonPBValue = Convert.ToBoolean(this.GetAndCheckValueString(ShowAcceptButtonDefaultValue, "ShowAcceptButtonPBValue"));
            this.ShowDeclineButtonPBValue = Convert.ToBoolean(this.GetAndCheckValueString(ShowDeclineButtonDefaultValue, "ShowDeclineButtonPBValue"));
            this.ShowResetButtonPBValue = Convert.ToBoolean(this.GetAndCheckValueString(ShowResetButtonDefaultValue, "ShowResetButtonPBValue"));

            if (!string.IsNullOrEmpty(Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, "DisplayDiscreetLinkPBValue")))
            {
                this.DisplayDiscreetLinkPBValue = Convert.ToBoolean(Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, "DisplayDiscreetLinkPBValue"));
            }

            if (!string.IsNullOrEmpty(Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, "UseLargeOverlayPBValue")))
            {
                this.UseLargeOverlayPBValue = Convert.ToBoolean(Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, "UseLargeOverlayPBValue"));
            }

            this.AnalyticsMessagePBValue = this.GetAndCheckValueString(AnalyticsMessageDefaultValue, "AnalyticsMessagePBValue");
            this.CookiePrivacyLinkMessagePBValue = this.GetAndCheckValueString(CookiePrivacyLinkMessageDefaultValue, "CookiePrivacyLinkMessagePBValue");
            this.ErrorMessagePBValue = this.GetAndCheckValueString(ErrorMessageDefaultValue, "ErrorMessagePBValue");
            this.PrivacyPolicyMessagePBValue = this.GetAndCheckValueString(PrivacyPolicyMessageDefaultValue, "PrivacyPolicyMessagePBValue");
            this.DifferentMessageOnPolicyPagePBValue = Convert.ToBoolean(this.GetAndCheckValueString(DifferentMessageOnPolicyPageDefaultValue, "DifferentMessageOnPolicyPagePBValue"));

            this.PrivacyPolicyLinkPBValue = this.GetAndCheckValueString(PrivacyPolicyLinkDefaultValue, "PrivacyPolicyLinkPBValue");
            this.WhatAreCookiesLinkPBValue = this.GetAndCheckValueString(WhatAreCookiesLinkDefaultValue, "WhatAreCookiesLinkPBValue");


            if (!string.IsNullOrEmpty(Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, "DisplayNotificationAtBottomOfPagePBValue")))
            {
                this.DisplayNotificationAtBottomOfPagePBValue = Convert.ToBoolean(Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, "DisplayNotificationAtBottomOfPagePBValue"));
            }

            this.DiscreetLinkPositionPBValue = Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, "DiscreetLinkPositionPBValue");

            if (!string.IsNullOrEmpty(Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, "EnablePageElementDisablerPBValue")))
            {
                this.EnablePageElementDisablerPBValue = Convert.ToBoolean(Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, "EnablePageElementDisablerPBValue"));
            }

            if (!string.IsNullOrEmpty(Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, "ActivatePageDisablerWhenVisitorDeclinesPBValue")))
            {
                this.ActivatePageDisablerWhenVisitorDeclinesPBValue = Convert.ToBoolean(Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, "ActivatePageDisablerWhenVisitorDeclinesPBValue"));
            }

            this.DisabledCSSSelectorsPBValue = this.GetAndCheckValueString(string.Empty, "DisabledCSSSelectorsPBValue");
        }

        /// <summary>
        /// The btn apply_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        void BtnApply_Click(object sender, EventArgs e)
        {
            this.saveProperties();
            SPUtility.Redirect("settings.aspx", SPRedirectFlags.UseSource | SPRedirectFlags.RelativeToLayoutsPage, HttpContext.Current);
        }

        /// <summary>
        /// The btn save_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        void BtnSave_Click(object sender, EventArgs e)
        {
            this.saveProperties();
            SPUtility.Redirect("LifeInSharePoint.EUCookie/admin/CookieCuttr.aspx", SPRedirectFlags.UseSource | SPRedirectFlags.RelativeToLayoutsPage, HttpContext.Current);
        }

        /// <summary>
        /// The save properties.
        /// </summary>
        private void saveProperties()
        {
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "GoogleAnalyticsIDPBValue", this.txtGoogleAnalyticsID.Text);
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "EnableGoogleAnalyticsUntilDeclinedPBValue", this.cbxEnableGoogleAnalyticsUntilDeclined.Checked.ToString());
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "DomainNamePBValue", this.txtDomainName.Text);

            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "ToggleMessagesPBValue", this.cbxToggleMessages.Checked.ToString());

            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "AcceptCookieButtonTextPBValue", this.txtAcceptCookieButtonText.Text);
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "DeclineCookieButtonTextPBValue", this.txtDeclineCookieButtonText.Text);
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "ResetCookieButtonTextPBValue", this.txtResetCookieButtonText.Text);
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "WhatAreCookieLinkTextPBValue", this.txtWhatAreCookieLinkText.Text);
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "DiscreetLinkTextPBValue", this.txtDiscreetLinkText.Text);

            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "ShowAcceptButtonPBValue", this.cbxShowAccept.Checked.ToString());
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "ShowDeclineButtonPBValue", this.cbxShowDecline.Checked.ToString());
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "ShowResetButtonPBValue", this.cbxShowReset.Checked.ToString());


            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "DisplayDiscreetLinkPBValue", this.cbxDisplayDiscreetLink.Checked.ToString());
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "UseLargeOverlayPBValue", this.cbxUseLargeOverlay.Checked.ToString());

            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "AnalyticsMessagePBValue", this.txtAnalyticsMessage.Text);
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "CookiePrivacyLinkMessagePBValue", this.txtCookiePrivacyLinkMessage.Text);
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "ErrorMessagePBValue", this.txtErrorMessage.Text);
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "PrivacyPolicyMessagePBValue", this.txtPrivacyPolicyMessage.Text);
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "DifferentMessageOnPolicyPagePBValue", this.cbxDifferentMessageOnPolicyPage.Checked.ToString());


            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "PrivacyPolicyLinkPBValue", this.txtPrivacyPolicyLink.Text);
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "WhatAreCookiesLinkPBValue", this.txtWhatAreCookiesLink.Text);


            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "DisplayNotificationAtBottomOfPagePBValue", this.cbxDisplayNotificationAtBottomOfPage.Checked.ToString());
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "DiscreetLinkPositionPBValue", this.ddlDiscreetLinkPosition.SelectedValue);


            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "EnablePageElementDisablerPBValue", this.cbxEnablePageElementDisabler.Checked.ToString());
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "ActivatePageDisablerWhenVisitorDeclinesPBValue", this.cbxActivatePageDisablerWhenVisitorDeclines.Checked.ToString());
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "DisabledCSSSelectorsPBValue", this.txtDisabledCSSSelectors.Text);
        }
    }
}
